#include "AreaChartGUI.h"
//#include "../PlotWindow/PlotWindow.h"
#include "../../common/utility.h"
#include "../GlobalChartOptions/GlobalChartOptions.h"
#include <QMessageBox>

namespace LightChart
{
    AreaChartGUI::AreaChartGUI(QWidget* parent = 0)
    : QDialog(parent)
    {
	setupUi(this);
	const GlobalChartSettings& global = GlobalChartSettings::get_instance();
	lineEditTitle->setText(global.chartTitle.c_str());
	lineEditWidth->setText(QString::number(global.width));
	lineEditHeight->setText(QString::number(global.height));
	graphicsView->setStyleSheet(getBgColorStyleSheet(global.bgColor));
	setupConnections();
    }

    void AreaChartGUI::setupConnections()
    {
	connect(pushButtonBgColor,SIGNAL(clicked()),this,SLOT(onButtonBgColorClicked()));
	connect(cancelButton,SIGNAL(clicked()),this,SLOT(onButtonCancelClicked()));
	connect(saveButton,SIGNAL(clicked()),this,SLOT(onButtonSaveClicked()));
    }

    void AreaChartGUI::onButtonBgColorClicked()
    {
	color = QColorDialog::getColor(Qt::white);
	graphicsView->setStyleSheet(getBgColorStyleSheet(color));
    }
    void AreaChartGUI::onButtonCancelClicked()
    {
	hide();
    }
    void AreaChartGUI::onButtonSaveClicked()
    {
	try
	{
	    if(!lineEditWidth->text().isEmpty())
	    {
		double tmp = QStringToDouble(lineEditWidth->text());
		if(tmp < 100 || tmp > 1920) throw std::runtime_error("Wrong width specified. Width must be in [100 : 1920]");
	    }
	    if(!lineEditHeight->text().isEmpty())
	    {
		double tmp = QStringToDouble(lineEditHeight->text());
		if(tmp < 50 || tmp > 1080) throw std::runtime_error("Wrong height specified. Height must be in [50 : 1080]");
	    }
	    hide();
	}
	catch(const std::runtime_error& e)
	{
	    QMessageBox::critical(this,"Input data corrupted.",e.what());
	}
    }
}

